'use strict';

const Controller = require('egg').Controller;

class DoRegisterController extends Controller {
  async index() {
    const {ctx} = this;
    console.log(ctx.request.body);
    var {username,pwd} = ctx.request.body;
    var db = this.app.mongodb;
    const collection = await db.collection("user");
    var res = await collection.find({username}).toArray();
    // console.log(res);
    if(res.length){
        ctx.body = "<script>alert('用户名已经被注册');location.href='/register'</script>"
    }else{
        ctx.cookies.set("register","success")
        var res = await collection.insertOne({username,pwd})
        ctx.body = "<script>alert('注册成功，请登录');location.href='/login'</script>"
    }
  }
}

module.exports = DoRegisterController;
